package com.iversonx.facade.example_1.service;

import com.iversonx.facade.example_1.remote.CostingService;
import com.iversonx.facade.example_1.remote.InventoryService;
import com.iversonx.facade.example_1.remote.LogisticsService;
import com.iversonx.facade.example_1.remote.OrderVerifyService;
import com.iversonx.facade.example_1.remote.PaymentGatewayService;
import com.iversonx.facade.example_1.remote.domain.OrderDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 **/
@Service
public class ClientService {
    @Autowired
    private CostingService costingService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private OrderVerifyService orderVerifyService;
    @Autowired
    private PaymentGatewayService paymentGatewayService;
    
    public void submitOrder() {
        // 创建订单信息
        OrderDetails orderDetails = new OrderDetails("Java Design Pattern book",
                "Simplified book on design patterns in Java",
                500, 10, "Street No 1", "Educational Area", 1212,
                "8811123456");
    
        // 更新库存.
        inventoryService.update(orderDetails.getProductNo());
    
        // 验证订单信息
        orderVerifyService.verifyShippingAddress(orderDetails.getPinCode());
    
        // 计算折扣
        orderDetails.setPrice(
                costingService.applyDiscount(
                        orderDetails.getPrice(),
                        orderDetails.getDiscountPercent()
                )
        );
    
        // 通过支付网关（如卡验证）从卡中收费
        paymentGatewayService.verifyCardDetails(orderDetails.getCardNo());
        paymentGatewayService.processPayment(orderDetails.getCardNo(), orderDetails.getPrice());
    
        // 通过提供物流来完成订单
        String shippingAddress = String.format("%s, %s - %d",
                orderDetails.getAddressLine1(),
                orderDetails.getAddressLine2(),
                orderDetails.getPinCode());
        logisticsService.shipProducts(orderDetails.getProductName(), shippingAddress);
    }
}
